/*
Date: October 2025
Project: Income and Child Maltreatment: Evidence from a Discontinuity in Tax Benefits
Author: Katherine Rittenhouse
Purpose: This file prepares the pre-standardized death files (years 1999 - 2020) for merge.
Files in: DSMF_standardized_final
Files out: Deaths_99_20
*/

clear all
set more off

import delim "DSMF_standardized_final.csv",clear

tostring dcstate dclocal dcyear,force replace 
replace dcstateid = "" if dcstateid=="."
replace dclocal = "" if dclocal=="."

gen lstate=strlen(dcstate)
gen llocal = strlen(dclocal)
gen lyr=strlen(dcyear)


replace dcstateid = "0" + dcstateid if lstate==5
replace dcstateid = "00" + dcstateid if lstate==4
replace dcstateid = "000" + dcstateid if lstate==3
replace dcstateid = "0000" + dcstateid if lstate==2
replace dcstateid = "00000" + dcstateid if lstate==1
replace dcstateid = "000000" if lstate==0

replace dclocal = "0" + dclocal if llocal==5
replace dclocal = "00" + dclocal if llocal==4
replace dclocal = "000" + dclocal if llocal==3
replace dclocal = "0000" + dclocal if llocal==2
replace dclocal = "00000" + dclocal if llocal==1
replace dclocal = "000000" if llocal==0

gen did = dcyear+dcstate+dclocal
gen length=strlen(did)
tab length

drop if dclocal=="000000"&dcstate=="000000"
gen deathd = substr(dcdtd,4,2)
gen deathm = substr(dcdtd,1,2)
gen deathy = substr(dcdtd,7,4)

destring deathd deathm deathy,force replace 

keep did deathd deathm deathy dcund dcdthtyp


save "Deaths_99_20.dta",replace

